﻿Imports System
Imports System.ComponentModel
Imports System.Globalization
Imports System.Workflow.Activities.Rules
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Activities.Rules.Design
    Friend Class RuleSetReferenceTypeConverter
        Inherits TypeConverter
        ' Methods
        Public Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal sourceType As Type) As Boolean
            Return ((sourceType Is GetType(String)) OrElse MyBase.CanConvertFrom(context, sourceType))
        End Function

        Public Overrides Function CanConvertTo(ByVal context As ITypeDescriptorContext, ByVal destinationType As Type) As Boolean
            Return ((destinationType Is GetType(String)) OrElse MyBase.CanConvertTo(context, destinationType))
        End Function

        Public Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal valueToConvert As Object) As Object
            If (context Is Nothing) Then
                Throw New ArgumentNullException("context")
            End If
            Dim key As String = [TryCast](valueToConvert,String)
            If ((key Is Nothing) OrElse (key.TrimEnd(New Char(0  - 1) {}).Length = 0)) Then
                key = String.Empty
            End If
            Dim serviceProvider As ISite = PropertyDescriptorUtils.GetSite(context, context.Instance)
            If (serviceProvider Is Nothing) Then
                Throw New InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Messages.MissingService, New Object() { GetType(ISite).FullName }))
            End If
            Dim ruleSets As RuleSetCollection = Nothing
            Dim definitions As RuleDefinitions = ConditionHelper.Load_Rules_DT(serviceProvider, Helpers.GetRootActivity([TryCast](serviceProvider.Component,Activity)))
            If (Not definitions Is Nothing) Then
                ruleSets = definitions.RuleSets
            End If
            If (((Not ruleSets Is Nothing) AndAlso (key.Length <> 0)) AndAlso Not ruleSets.Contains(key)) Then
                Dim item As New RuleSet { _
                    .Name = key _
                }
                ruleSets.Add(item)
                ConditionHelper.Flush_Rules_DT(serviceProvider, Helpers.GetRootActivity([TryCast](serviceProvider.Component,Activity)))
            End If
            Return New RuleSetReference { _
                .RuleSetName = key _
            }
        End Function

        Public Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object, ByVal destinationType As Type) As Object
            If (destinationType Is Nothing) Then
                Throw New ArgumentNullException("destinationType")
            End If
            If (Not destinationType Is GetType(String)) Then
                Return MyBase.ConvertTo(context, culture, value, destinationType)
            End If
            Dim reference As RuleSetReference = [TryCast](value,RuleSetReference)
            If (Not reference Is Nothing) Then
                Return reference.RuleSetName
            End If
            Return Nothing
        End Function

        Public Overrides Function GetProperties(ByVal context As ITypeDescriptorContext, ByVal value As Object, ByVal attributes As Attribute()) As PropertyDescriptorCollection
            Dim serviceProvider As ISite = Nothing
            Dim component As IComponent = PropertyDescriptorUtils.GetComponent(context)
            If (Not component Is Nothing) Then
                serviceProvider = component.Site
            End If
            Dim descriptors As New PropertyDescriptorCollection(Nothing)
            descriptors.Add(New RuleSetPropertyDescriptor(serviceProvider, TypeDescriptor.CreateProperty(GetType(RuleSet), "RuleSet Definition", GetType(RuleSet), New Attribute() { New DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Content), DesignOnlyAttribute.Yes })))
            Return descriptors
        End Function

        Public Overrides Function GetPropertiesSupported(ByVal context As ITypeDescriptorContext) As Boolean
            Return True
        End Function

    End Class
End Namespace

